package com.atguigu.fink.chapter01.hive;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

/**
 * @Author lzc
 * @Date 2022/11/28 09:06
 */
public class Flink01_Hive {
    public static void main(String[] args) throws Exception {
        // 大小写切换:shift + ctrl + u
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    
        /*System.out.println(tEnv.getCurrentCatalog());
        System.out.println(tEnv.getCurrentDatabase());
    
        DataStreamSource<WaterSensor> stream = env.fromElements(
            new WaterSensor("s1", 1L, 10),
            new WaterSensor("s1", 2L, 10),
            new WaterSensor("s2", 3L, 20),
            new WaterSensor("s1", 4L, 30),
            new WaterSensor("s1", 5L, 40),
            new WaterSensor("s1", 6L, 50)
        );
        Table table = tEnv.fromDataStream(stream);
        tEnv.createTemporaryView("sensor", table);
    
        tEnv.sqlQuery("select * from default_catalog.default_database.sensor").execute().print();
    */
        
        // 1. 先创建 HiveCatalog 对象
        HiveCatalog hive = new HiveCatalog("hive", "gmall", "input/");
        // 2. 给系统注册 hiveCatalog
        tEnv.registerCatalog("hive", hive);
    
        //System.out.println(tEnv.getCurrentCatalog());
        //System.out.println(tEnv.getCurrentDatabase());
    
        tEnv.useCatalog("hive");
        tEnv.useDatabase("gmall");
        //System.out.println(tEnv.getCurrentCatalog());
        //System.out.println(tEnv.getCurrentDatabase());
    
       /* for (String c : tEnv.listCatalogs()) {
            System.out.println(c);
        }*/
        Table result = tEnv.sqlQuery("select * from gmall.person");
    
        tEnv.executeSql("insert into person select * from " + result);
    
    
    }
}
